//
//  SetupViewController.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/4.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation


@objc class SetupViewController: BaseViewController {
    @objc static func viewController() -> SetupViewController {
        return SetupViewController()
    }
    
    @IBOutlet var cacheLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        Log.debug("view did load")
        
        self.title = "设置"
        updateCacheSizeText()
    }
    
    
    @IBAction func btnSysPermissionClick(btn: UIButton) {
        Log.debug("btn sys permission click")
        
        let url = URL.init(string: "App-prefs:root=General")
        if (UIApplication.shared.canOpenURL(url!)) {
            UIApplication.shared.open(url!)
        }
    }
    
    @IBAction func btnCleanCacheClick(btn: UIButton) {
        Log.debug("btn clean cache click")
        
        let hud = HUD.showLoading(msg: "正在清理...", delay: 0, in: self.view)
        SDImageCache.shared.clearDisk {
            hud.hide(animated: true)
        }
        
        updateCacheSizeText()
    }
    
    private func updateCacheSizeText() {
        let size: Float = Float(SDImageCache.shared.totalDiskSize())
        let temp = Float(1024 * 1024.0)
        let result = Float(size / temp)
        self.cacheLabel.text = String.init(format: "%.2f M", result)
    }
}

